package com.bocloud.cmp.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.bocloud.cmp.dao.SubnetDao;
import com.bocloud.cmp.entity.resource.Subnet;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.BasicDao;
import com.bocloud.database.utils.SQLHelper;

/**
 * 子网DAO接口实现
 * 
 * @author dongkai
 *
 */
@Repository("subnetDao")
public class SubnetDaoImpl extends BasicDao implements SubnetDao {

	@Override
	public List<Subnet> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select * from subnet where 1=1";
		sql = SQLHelper.buildSql(sql, page, rows, params, sorter, "");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		List<Object> list = this.queryForList(sql, paramMap, Subnet.class);
		List<Subnet> subnets = new ArrayList<>();
		Subnet subnet = null;
		for (Object obj : list) {
			subnet = (Subnet) obj;
			subnets.add(subnet);
		}
		return subnets;
	}

	@Override
	public List<Subnet> list(Long vendorId, String networkId) throws Exception {
		String sql = "select * from subnet where vendor_id = :vendorId and network_id = :networkId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put("networkId", networkId);
		List<Object> list = this.queryForList(sql, params, Subnet.class);
		List<Subnet> subnets = new ArrayList<>();
		Subnet subnet = null;
		for (Object obj : list) {
			subnet = (Subnet) obj;
			subnets.add(subnet);
		}
		return subnets;
	}

	@Override
	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from subnet where 1=1";
		sql = SQLHelper.buildSql(sql, params, null, "");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		return this.countQuery(sql, paramMap).intValue();
	}

	@Override
	public Subnet save(Subnet subnet) throws Exception {
		return (Subnet) this.saveEntity(subnet);
	}

	@Override
	public boolean modify(Subnet subnet) throws Exception {
		return this.updateEntity(subnet);
	}

	@Override
	public boolean delete(Long id) throws Exception {
		String sql = "delete from subnet where id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		return this.update(sql, params) > 0;
	}

	@Override
	public Subnet query(Long id) throws Exception {
		String sql = "select * from subnet where id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<Object> list = this.queryForList(sql, params, Subnet.class);
		if (list.isEmpty()) {
			return null;
		}
		return (Subnet) list.get(0);
	}

	@Override
	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.id,a.name,a.subnet_id from subnet a where 1=1";
		sql = SQLHelper.buildSql(sql, 1, Integer.MAX_VALUE, params, sorter, "a");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		List<Object> list = this.queryForList(sql, paramMap, Subnet.class);
		List<SimpleBean> beans = new ArrayList<>();
		Subnet subnet = null;
		for (Object obj : list) {
			subnet = (Subnet) obj;
			beans.add(new SimpleBean(subnet.getId(), subnet.getName(), subnet.getSubnetId()));
		}
		return beans;
	}

	@Override
	public boolean removeByVid(Long vendorId) throws Exception {
		String sql = "delete from subnet where vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		return this.update(sql, params) > 0;
	}

	@Override
	public List<Subnet> listByVid(Long vendorId) throws Exception {
		String sql = "select * from subnet where vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		List<Object> list = this.queryForList(sql, params, Subnet.class);
		List<Subnet> subnets = new ArrayList<>();
		Subnet subnet = null;
		for (Object obj : list) {
			subnet = (Subnet) obj;
			subnets.add(subnet);
		}
		return subnets;
	}

}
